package com.dream.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dream.common.constans.RedisKeysDefine;
import com.dream.common.enums.ReportedDataActionEnum;
import com.dream.common.exception.RespErrorException;
import com.dream.common.utils.*;
import com.dream.common.utils.payutils.*;
import com.dream.common.utils.payutils.bean.*;
import com.dream.common.utils.payutils.bean.configuration.FunctionSwitchBean;
import com.dream.common.utils.payutils.bean.configuration.NextSenderRuleBean;
import com.dream.common.utils.pickutils.NumberUtil;
import com.dream.dto.DataResponse;
import com.dream.dto.socket.SocketBean;
import com.dream.entity.*;
import com.dream.entity.AppMessageBean;
import com.dream.mapper.*;
import com.dream.service.DelayRelayQueue;
import com.dream.service.basic.AsyncInvokeService;
import com.dream.service.basic.BaseServiceHelper;
import com.dream.service.basic.RedisCacheService;
import com.dream.web.aop.annotation.CommonResponse;
import com.github.binarywang.java.emoji.EmojiConverter;
import io.swagger.annotations.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/app")
@Api(tags = "app相关接口")
public class AppController {

    @Resource
    private GroupRulesMapper groupRulesMapper;
    @Resource
    private GroupMemberMapper memberMapper;
    @Resource
    private AsyncInvokeService asyncInvokeService;
    @Resource
    private RedisCacheService redisCacheService;
    @Resource
    private RedPackageSendMapper sendMapper;
    @Resource
    private RedPackagePickMapper pickMapper;
    @Resource
    private DelayRelayQueue relayQueue;

    @CommonResponse
    @GetMapping(value = "/checkUpdate/{version}")
    @ApiOperation(value = "检查更新", response = DataResponse.class)
    public Object checkUpdate(@PathVariable String version) {
        /*checkUpdateResponse.isUpdate = 2;
        if (checkUpdateRequest.version < 0) {
            checkUpdateResponse.isUpdate = 1;
            checkUpdateResponse.title = "云笔记2.0更新";
            checkUpdateResponse.content = "1,新增功能1" + "\n2,新增功能2";
            checkUpdateResponse.updateType = 1;
            checkUpdateResponse.updateUrl = "http://www.baidu.com";
        }*/
        return null;
    }

    @CommonResponse
    @PostMapping(value = "/getHomePageData")
    @ApiOperation(value = "app获取首页数据", response = PayBean.class)
    public Object getHomePageData() {
        String user = ThreadContextUtil.get("user").toString();
        HomePageDataBean homePageDataBean = new HomePageDataBean();
        BaseConfig baseConfig = RedisCacheUtils.getBaseConfig(user);
        if (baseConfig == null) {
            homePageDataBean.baseConfigTag = "未设置";
        } else {
            homePageDataBean.baseConfigTag = "已设置";
        }
        List<String> keyListCan = redisCacheService.getKeyListByKey(RedisKeysDefine.PLAY_RULE + user + ":" + RedisKeysDefine.BASE_PLAY_CAN);
        if (keyListCan == null || keyListCan.size() == 0) {
            homePageDataBean.canPicConfigTag = "未设置";
        } else {
            homePageDataBean.canPicConfigTag = "已设置";
        }
        List<String> keyListCant = redisCacheService.getKeyListByKey(RedisKeysDefine.PLAY_RULE + user + ":" + RedisKeysDefine.BASE_PLAY_CANT);
        if (keyListCant == null || keyListCant.size() == 0) {
            homePageDataBean.cantPicConfigTag = "未设置";
        } else {
            homePageDataBean.cantPicConfigTag = "已设置";
        }
        List<String> keyListJie = redisCacheService.getKeyListByKey(RedisKeysDefine.PLAY_RULE + user + ":" + RedisKeysDefine.BASE_PLAY_JIE);
        if (keyListJie == null || keyListJie.size() == 0) {
            homePageDataBean.jieLongConfigTag = "未设置";
        } else {
            homePageDataBean.jieLongConfigTag = "已设置";
        }
        RiMoneyBean riMoneyBean = redisCacheService.getObject(RedisKeysDefine.BASE_PLAY_RI + user, RiMoneyBean.class);
        if (riMoneyBean == null) {
            homePageDataBean.riConfigTag = "未设置";
        } else {
            homePageDataBean.riConfigTag = "已设置";
        }
        List<MemberBean> managerMemberList = redisCacheService.getObjectList(RedisKeysDefine.MEMBER_LIST + user + "--" + RedisKeysDefine.MEMBER_TYPE_MANAGER, MemberBean.class);
        if (managerMemberList == null || managerMemberList.size() == 0) {
            homePageDataBean.managerConfigTag = "未设置";
        } else {
            homePageDataBean.managerConfigTag = "已设置" + managerMemberList.size() + "个管理员";
        }
        List<MemberBean> pickerMemberList = redisCacheService.getObjectList(RedisKeysDefine.MEMBER_LIST + user + "--" + RedisKeysDefine.MEMBER_TYPE_PICKER, MemberBean.class);
        if (pickerMemberList == null || pickerMemberList.size() == 0) {
            homePageDataBean.pickerConfigTag = "未设置";
        } else {
            homePageDataBean.pickerConfigTag = "已设置" + pickerMemberList.size() + "个抢包手";
        }
        List<MemberBean> tuoMemberList = redisCacheService.getObjectList(RedisKeysDefine.MEMBER_LIST + user + "--" + RedisKeysDefine.MEMBER_TYPE_TUO, MemberBean.class);
        if (tuoMemberList == null || tuoMemberList.size() == 0) {
            homePageDataBean.tuoHaoConfigTag = "未设置";
        } else {
            homePageDataBean.tuoHaoConfigTag = "已设置" + tuoMemberList.size() + "个托号";
        }
        List<MemberBean> blackerMemberList = redisCacheService.getObjectList(RedisKeysDefine.MEMBER_LIST + user + "--" + RedisKeysDefine.MEMBER_TYPE_BLACKER, MemberBean.class);
        if (blackerMemberList == null || blackerMemberList.size() == 0) {
            homePageDataBean.blackerConfigTag = "未设置";
        } else {
            homePageDataBean.blackerConfigTag = "已设置" + blackerMemberList.size() + "个黑名単";
        }
        MemberBean noDeadMember = redisCacheService.getObject(RedisKeysDefine.NO_DEAD_INFO + user, MemberBean.class);
        if (noDeadMember == null) {
            homePageDataBean.noDeadConfigTag = "未设置";
        } else {
            homePageDataBean.noDeadConfigTag = noDeadMember.name;
        }
        AutoPayConfig autoPayConfig = redisCacheService.getObject(RedisKeysDefine.ROBOT_AUTO_PAY_CONFIG + user, AutoPayConfig.class);
        if (autoPayConfig == null) {
            homePageDataBean.autoPayConfigTag = "未设置";
        } else {
            homePageDataBean.autoPayConfigTag = "已设置";
        }
        return homePageDataBean;
    }

    @CommonResponse
    @PostMapping(value = "/uploadPlayRule")
    @ApiOperation(value = "app上传晥法设置", response = PayBean.class)
    public Object uploadPlayRule(@RequestBody List<PlayRuleBean> ruleBeanList) {
        String user = ThreadContextUtil.get("user").toString();
        for (PlayRuleBean playRuleBean : ruleBeanList) {
            RedisCacheUtils.setPlayRule(RedisKeysDefine.PLAY_RULE + user + ":" + playRuleBean.basePlayType + "--" + playRuleBean.packageNumber + "--" + playRuleBean.playType, playRuleBean);
        }
        return 1;
    }

    @CommonResponse
    @PostMapping(value = "/getPlayRule")
    @ApiOperation(value = "app获取晥法设置", response = PayBean.class)
    public Object getPlayRule(@RequestBody JSONObject jsonObject) {
        String packageNumber = jsonObject.getString("packageNumber");
        String basePlayType = jsonObject.getString("basePlayType");
        String user = ThreadContextUtil.get("user").toString();
        List<String> keyList = redisCacheService.getKeyListByKey(RedisKeysDefine.PLAY_RULE + user + ":" + basePlayType + "--" + packageNumber);
        if (keyList == null) {
            keyList = new ArrayList<>();
        }
        List<PlayRuleBean> ruleBeanList = new ArrayList<>();
        for (String beginKey : keyList) {
            ruleBeanList.add(redisCacheService.getObject(beginKey, PlayRuleBean.class));
        }
        return ruleBeanList;
    }

    @CommonResponse
    @PostMapping(value = "/uploadReportConfig")
    @ApiOperation(value = "app上传报奖设置", response = PayBean.class)
    public Object uploadReportConfig(@RequestBody ReportConfig reportConfig) {
        String user = ThreadContextUtil.get("user").toString();
        redisCacheService.setObject(RedisKeysDefine.REPORT_CONFIG + user, reportConfig);
        return 1;
    }

    @CommonResponse
    @PostMapping(value = "/getReportConfig")
    @ApiOperation(value = "app获取报奖设置", response = PayBean.class)
    public Object getReportConfig() {
        String user = ThreadContextUtil.get("user").toString();
        return redisCacheService.getObject(RedisKeysDefine.REPORT_CONFIG + user, ReportConfig.class);
    }

    @CommonResponse
    @PostMapping(value = "/uploadBaseConfig")
    @ApiOperation(value = "app上传通用设置", response = PayBean.class)
    public Object uploadBaseConfig(@RequestBody BaseConfig baseConfig) {
        String user = ThreadContextUtil.get("user").toString();
        RedisCacheUtils.setBaseConfig(user, baseConfig);
        return 1;
    }

    @CommonResponse
    @PostMapping(value = "/getBaseConfig")
    @ApiOperation(value = "app获取通用设置", response = PayBean.class)
    public Object getBaseConfig() {
        String user = ThreadContextUtil.get("user").toString();
        return redisCacheService.getObject(RedisKeysDefine.BASE_CONFIG + user, BaseConfig.class);
    }

    @CommonResponse
    @PostMapping(value = "/uploadMemberList")
    @ApiOperation(value = "app上传成员列表", response = PayBean.class)
    public Object uploadMemberList(@RequestBody JSONObject jsonObject) {
        String user = ThreadContextUtil.get("user").toString();
        int memberType = jsonObject.getIntValue("memberType");
        JSONArray jsonArray = jsonObject.getJSONArray("MemberList");
        List<MemberBean> memberBeanList = JSONObject.parseArray(jsonArray.toJSONString(), MemberBean.class);
        RedisCacheUtils.setUserListByType(user, memberType, memberBeanList);
        return 1;
    }

    @CommonResponse
    @PostMapping(value = "/getMemberList")
    @ApiOperation(value = "app获取成员列表", response = PayBean.class)
    public Object getMemberList(@RequestBody JSONObject jsonObject) {
//        public static final String MEMBER_TYPE_NAME_MANAGER = "管理员";
//        public static final String MEMBER_TYPE_NAME_PICKER = "抢包手";
//        public static final String MEMBER_TYPE_NAME_TUO = "托号";
//        public static final String MEMBER_TYPE_NAME_BLACKER = "黑名単";
        String user = ThreadContextUtil.get("user").toString();
        int memberType = jsonObject.getIntValue("memberType");
        List<MemberBean> memberBeanList = RedisCacheUtils.getUserListByType(user, memberType);
        if (memberBeanList == null) {
            memberBeanList = new ArrayList<>();
        }
        return memberBeanList;
    }

    @CommonResponse
    @PostMapping(value = "/uploadNoDeadInfo")
    @ApiOperation(value = "app上传免死信息", response = PayBean.class)
    public Object uploadNoDeadInfo(@RequestBody MemberBean memberBean) {
        String user = ThreadContextUtil.get("user").toString();
        RedisCacheUtils.setNoDead(user, memberBean);
        return 1;
    }

    @CommonResponse
    @PostMapping(value = "/getNoDeadInfo")
    @ApiOperation(value = "app获取免死信息", response = PayBean.class)
    public Object getNoDeadInfo() {
        String user = ThreadContextUtil.get("user").toString();
        return RedisCacheUtils.getNoDead(user);
    }

    @CommonResponse
    @PostMapping(value = "/uploadAutoPayConfig")
    @ApiOperation(value = "app上传自动赔付设置", response = PayBean.class)
    public Object uploadAutoPayConfig(@RequestBody AutoPayConfig autoPayConfig) {
        String user = ThreadContextUtil.get("user").toString();
        redisCacheService.setObject(RedisKeysDefine.ROBOT_AUTO_PAY_CONFIG + user, autoPayConfig);
        return 1;
    }

    @CommonResponse
    @PostMapping(value = "/getAutoPayConfig")
    @ApiOperation(value = "app获取自动赔付设置", response = PayBean.class)
    public Object getAutoPayConfig() {
        String user = ThreadContextUtil.get("user").toString();
        return redisCacheService.getObject(RedisKeysDefine.ROBOT_AUTO_PAY_CONFIG + user, AutoPayConfig.class);
    }

    @CommonResponse
    @PostMapping(value = "/uploadSpecialMoneyConfig")
    @ApiOperation(value = "app上传自特殊金娥奖励设置", response = PayBean.class)
    public Object uploadSpecialMoneyConfig(@RequestBody RiMoneyBean riMoneyBean) {
        String user = ThreadContextUtil.get("user").toString();
        RedisCacheUtils.setRiMoneyBean(user, riMoneyBean);
        return 1;
    }

    @CommonResponse
    @PostMapping(value = "/getSpecialMoneyConfig")
    @ApiOperation(value = "app获取自特殊金娥奖励设置", response = PayBean.class)
    public Object getSpecialMoneyConfig() {
        String user = ThreadContextUtil.get("user").toString();
        RiMoneyBean riMoneyBean = RedisCacheUtils.getRiMoneyBean(RedisKeysDefine.BASE_PLAY_RI + user);
        return riMoneyBean;
    }

    @CommonResponse
    @PostMapping(value = "/queryPickerDataByUserId")
    @ApiOperation(value = "查我-查信息", response = PayBean.class)
    public Object queryPickerDataByUserId(@RequestBody AppMessageBean appMessageBean) {
        String user = ThreadContextUtil.get("user").toString();
        StringBuilder stringBuilder = new StringBuilder();
        BaseConfig baseConfig = RedisCacheUtils.getBaseConfig(user);
        if (baseConfig.queryMoneyType == 3) {
            stringBuilder.append("当前襟止查流水！");
            return stringBuilder.toString();
        }
        PickerOverViewBean senderOverViewBean = sendMapper.querySenderDataByUserId(user, appMessageBean.sendUserId);
        PickerOverViewBean pickerOverViewBean = pickMapper.queryPickerDataByUserId(user, appMessageBean.sendUserId);
        senderOverViewBean.pickMoney = pickerOverViewBean.pickMoney;
        senderOverViewBean.payMoney = pickerOverViewBean.payMoney;
        senderOverViewBean = senderOverViewBean.getFormatPickerOverViewBean(senderOverViewBean);
        stringBuilder.append("\n枪：" + pickerOverViewBean.pickMoney);
        stringBuilder.append("\n伐：" + senderOverViewBean.sendMoney);
        if (baseConfig.queryMoneyType == 1) {
            stringBuilder.append("\n賠：" + pickerOverViewBean.payMoney);
            stringBuilder.append("\n总瀛：" + (StringUtils.isEmpty(senderOverViewBean.profitMoney) ? "0" : senderOverViewBean.profitMoney));
        }
        return stringBuilder.toString();
    }

    @CommonResponse
    @PostMapping(value = "/uploadMessage")
    @ApiOperation(value = "app上传指令消息", response = PayBean.class)
    public Object uploadMessage(@RequestBody AppMessageBean appMessageBean) {
        String user = ThreadContextUtil.get("user").toString();
        BaseConfig baseConfig = RedisCacheUtils.getBaseConfig(user);
        List<MemberBean> managerList = RedisCacheUtils.getUserListByType(user, RedisKeysDefine.MEMBER_TYPE_MANAGER);
        if ("开启查询".equals(appMessageBean.message)) {
            StringBuilder stringBuilder = new StringBuilder();
            if (!managerList.contains(new MemberBean((appMessageBean.sendUserId)))) {
                stringBuilder.append("您不是管理员无权开启查询！");
                return stringBuilder.toString();
            }
            baseConfig.queryMoneyType = 2;
            RedisCacheUtils.setBaseConfig(user, baseConfig);
            stringBuilder.append("开启查询成功！");
            return stringBuilder.toString();
        } else if ("关闭查询".equals(appMessageBean.message)) {
            StringBuilder stringBuilder = new StringBuilder();
            if (!managerList.contains(new MemberBean((appMessageBean.sendUserId)))) {
                stringBuilder.append("您不是管理员无权关闭查询！");
                return stringBuilder.toString();
            }
            baseConfig.queryMoneyType = 3;
            RedisCacheUtils.setBaseConfig(user, baseConfig);
            stringBuilder.append("关闭查询成功！");
            return stringBuilder.toString();
        }
        return "";
    }


    @CommonResponse
    @PostMapping(value = "/sendGroupRule")
    @ApiOperation(value = "发送群规", response = PayBean.class)
    public Object sendGroupRule(@RequestBody AppMessageBean appMessageBean) {
        String user = ThreadContextUtil.get("user").toString();
        List<MemberBean> managerMemberList = RedisCacheUtils.getUserListByType(user, RedisKeysDefine.MEMBER_TYPE_MANAGER);
        boolean isManager = false;
        if (managerMemberList != null && managerMemberList.size() > 0) {
            for (MemberBean managerMember : managerMemberList) {
                if (managerMember.id.equals(appMessageBean.sendUserId)) {
                    isManager = true;
                    break;
                }
            }
        }
        if (isManager) {
            BaseConfig baseConfig = RedisCacheUtils.getBaseConfig(user);
            return baseConfig.groupRule;
        } else {
            return "你不是管理不可以发送群规";
        }
    }

    @CommonResponse
    @PostMapping(value = "/kickMember")
    @ApiOperation(value = "app踢出群成员", response = PayBean.class)
    public Object kickMember(@RequestBody AppMessageBean appMessageBean) {
        String user = ThreadContextUtil.get("user").toString();
        AliRPResultToApp resultToApp = new AliRPResultToApp();
        List<MemberBean> managerMemberList = RedisCacheUtils.getUserListByType(user, RedisKeysDefine.MEMBER_TYPE_MANAGER);
        boolean isManager = false;
        if (managerMemberList != null && managerMemberList.size() > 0) {
            for (MemberBean managerMember : managerMemberList) {
                if (managerMember.id.equals(appMessageBean.sendUserId)) {
                    isManager = true;
                    break;
                }
            }
        }
        if (isManager) {
            resultToApp.sendString = "踢成功：" + appMessageBean.atUserName;
            List<String> kickUserIdList = new ArrayList<>();
            kickUserIdList.add(appMessageBean.atUserId);
            resultToApp.kickUserIdList = kickUserIdList;
        } else {
            resultToApp.sendString = "你不是管理不可以踢人";
        }
        return resultToApp;
    }

    @CommonResponse
    @PostMapping(value = "/uploadNewMember")
    @ApiOperation(value = "app上传入群新成员信息", response = PayBean.class)
    public Object uploadNewMember(@RequestBody NewMemberIntoGroupBean newMemberIntoGroupBean) {
        String user = ThreadContextUtil.get("user").toString();
        List<MemberBean> blackerMemberList = RedisCacheUtils.getUserListByType(user, RedisKeysDefine.MEMBER_TYPE_BLACKER);
        List<MemberBean> managerMemberList = RedisCacheUtils.getUserListByType(user, RedisKeysDefine.MEMBER_TYPE_MANAGER);
        StringBuilder returnSB = new StringBuilder();
        AliRPResultToApp aliRPResultToApp = new AliRPResultToApp();
        boolean isManagerInvite = false;//管理员拉人
        if (managerMemberList != null && managerMemberList.size() > 0) {
            for (MemberBean managerMember : managerMemberList) {
                if (managerMember.id.equals(newMemberIntoGroupBean.inviteId)) {
                    isManagerInvite = true;
                    break;
                }
            }
        }
        if (!isManagerInvite) {
            BaseConfig baseConfig = RedisCacheUtils.getBaseConfig(user);
            boolean isBlackerInvitee = false;//被邀请人是否是黑名単人员
            if (blackerMemberList != null && blackerMemberList.size() > 0) {
                for (MemberBean blackerMember : blackerMemberList) {
                    if (blackerMember.id.equals(newMemberIntoGroupBean.inviteeId)) {
                        isBlackerInvitee = true;
                        break;
                    }
                }
            }
            if (isBlackerInvitee) {//黑名単进群openMemberInvite
                if (baseConfig.blackerInWarn) {
                    returnSB.append("黑名単：[" + newMemberIntoGroupBean.inviteeName + "]进入群聊");
                }
                if (baseConfig.blackerInKick) {
                    List<String> kickUserIdList = new ArrayList<>();
                    kickUserIdList.add(newMemberIntoGroupBean.inviteeId);
                    aliRPResultToApp.kickUserIdList = kickUserIdList;
                    if (!StringUtils.isEmpty(returnSB.toString())) {
                        returnSB.append("\n黑名単：[" + newMemberIntoGroupBean.inviteeName + "]进入群聊，已被踢出群");
                    } else {
                        returnSB.append("黑名単：[" + newMemberIntoGroupBean.inviteeName + "]进入群聊，已被踢出群");
                    }
                }
            }
        }
        aliRPResultToApp.sendString = returnSB.toString();
        return aliRPResultToApp;
    }

    @CommonResponse
    @PostMapping(value = "/deletePickerDataByUserId")
    @ApiOperation(value = "删除某个用户的数据", response = PayBean.class)
    public Object deletePickerDataByUserId(@RequestBody JSONObject jsonObject) {
        String user = ThreadContextUtil.get("user").toString();
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        String userId = jsonObject.getString("userId");
        sendMapper.deleteRedPackageSendInfoByUserId(user, userId, DateUtils.getTimeMillis(startTime), DateUtils.getTimeMillis(endTime));
        pickMapper.deleteRedPackagePickInfoByUserId(user, userId, DateUtils.getTimeMillis(startTime), DateUtils.getTimeMillis(endTime));
        return "";
    }

    @CommonResponse
    @PostMapping(value = "/managerDeleteUserDataByUserId")
    @ApiOperation(value = "管理员@删除某个用户的数据", response = PayBean.class)
    public Object managerDeleteUserDataByUserId(@RequestBody AppMessageBean appMessageBean) {
        String user = ThreadContextUtil.get("user").toString();
        List<MemberBean> managerMemberList = RedisCacheUtils.getUserListByType(user, RedisKeysDefine.MEMBER_TYPE_MANAGER);
        boolean isManager = false;
        if (managerMemberList != null && managerMemberList.size() > 0) {
            for (MemberBean managerMember : managerMemberList) {
                if (managerMember.id.equals(appMessageBean.sendUserId)) {
                    isManager = true;
                    break;
                }
            }
        }
        if (isManager) {
            sendMapper.deleteRedPackageSendInfoByUserId(user, appMessageBean.atUserId, 0, 1669819765000L);
            pickMapper.deleteRedPackagePickInfoByUserId(user, appMessageBean.atUserId, 0, 1669819765000L);
            return "清成功";
        } else {
            return "你不是管理不可以清数据";
        }
    }

    @CommonResponse
    @PostMapping(value = "/managerQueryUserDataByUserId")
    @ApiOperation(value = "管理员@查某个用户的数据", response = PayBean.class)
    public Object managerQueryUserDataByUserId(@RequestBody AppMessageBean appMessageBean) {
        String user = ThreadContextUtil.get("user").toString();
        List<MemberBean> managerMemberList = RedisCacheUtils.getUserListByType(user, RedisKeysDefine.MEMBER_TYPE_MANAGER);
        boolean isManager = false;
        if (managerMemberList != null && managerMemberList.size() > 0) {
            for (MemberBean managerMember : managerMemberList) {
                if (managerMember.id.equals(appMessageBean.sendUserId)) {
                    isManager = true;
                    break;
                }
            }
        }
        if (isManager) {
            StringBuilder stringBuilder = new StringBuilder();
            BaseConfig baseConfig = RedisCacheUtils.getBaseConfig(user);
            PickerOverViewBean senderOverViewBean = sendMapper.querySenderDataByUserId(user, appMessageBean.atUserId);
            PickerOverViewBean pickerOverViewBean = pickMapper.queryPickerDataByUserId(user, appMessageBean.atUserId);
            senderOverViewBean.pickMoney = pickerOverViewBean.pickMoney;
            senderOverViewBean.payMoney = pickerOverViewBean.payMoney;
            senderOverViewBean = senderOverViewBean.getFormatPickerOverViewBean(senderOverViewBean);
            stringBuilder.append("\n枪苞：" + pickerOverViewBean.pickMoney);
            stringBuilder.append("\n伐苞：" + senderOverViewBean.sendMoney);
            if (baseConfig.queryMoneyType == 1) {
                stringBuilder.append("\n賠付：" + pickerOverViewBean.payMoney);
                stringBuilder.append("\n总瀛：" + (StringUtils.isEmpty(senderOverViewBean.profitMoney) ? "0" : senderOverViewBean.profitMoney));
            }
            return stringBuilder.toString();
        } else {
            return "你不是管理不可以查数据";
        }
    }

    @CommonResponse
    @PostMapping(value = "/deleteOverViewData")
    @ApiOperation(value = "查询数据详情总览", response = PayBean.class)
    public Object deleteOverViewData(@RequestBody JSONObject jsonObject) {
        String user = ThreadContextUtil.get("user").toString();
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        sendMapper.deleteRedPackageSendInfo(user, DateUtils.getTimeMillis(startTime), DateUtils.getTimeMillis(endTime));
        pickMapper.deleteRedPackagePickInfo(user, DateUtils.getTimeMillis(startTime), DateUtils.getTimeMillis(endTime));
        return "";
    }

    @CommonResponse
    @PostMapping(value = "/queryOverViewData")
    @ApiOperation(value = "查询数据详情总览", response = PayBean.class)
    public Object queryOverViewData(@RequestBody JSONObject jsonObject) {
        String user = ThreadContextUtil.get("user").toString();
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        OverViewDataBean overViewDataBean = sendMapper.queryRedPackageSendInfo(user, DateUtils.getTimeMillis(startTime), DateUtils.getTimeMillis(endTime));
        overViewDataBean.profitMoney = NumberUtil.subtract(NumberUtil.subtract(NumberUtil.subtract(overViewDataBean.pickMoney, overViewDataBean.stealMoney), overViewDataBean.thunderMoney), overViewDataBean.rewardMoney);

        overViewDataBean.sendMoney = NumberUtil.divide(overViewDataBean.sendMoney, "1", 2);
        overViewDataBean.pickMoney = NumberUtil.divide(overViewDataBean.pickMoney, "1", 2);
        overViewDataBean.stealMoney = NumberUtil.divide(overViewDataBean.stealMoney, "1", 2);
        overViewDataBean.thunderMoney = NumberUtil.divide(overViewDataBean.thunderMoney, "1", 2);
        overViewDataBean.rewardMoney = NumberUtil.divide(overViewDataBean.rewardMoney, "1", 2);
        overViewDataBean.profitMoney = NumberUtil.divide(overViewDataBean.profitMoney, "1", 2);

        return overViewDataBean;
    }

    @CommonResponse
    @PostMapping(value = "/queryPickerData")
    @ApiOperation(value = "查询玩家数据", response = PayBean.class)
    public Object queryPickerData(@RequestBody JSONObject jsonObject) {
        String user = ThreadContextUtil.get("user").toString();
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        List<PickerOverViewBean> pickerOverViewBeanList = pickMapper.queryPickerDataList(user, DateUtils.getTimeMillis(startTime), DateUtils.getTimeMillis(endTime));
        List<PickerOverViewBean> senderOverViewBeanList = sendMapper.querySenderDataList(user, DateUtils.getTimeMillis(startTime), DateUtils.getTimeMillis(endTime));
        for (PickerOverViewBean picker : pickerOverViewBeanList) {//合并重复数据
            if (senderOverViewBeanList.contains(picker)) {
                PickerOverViewBean sender = senderOverViewBeanList.get(senderOverViewBeanList.indexOf(picker));
                picker.sendNumber = sender.sendNumber;
                picker.sendMoney = sender.sendMoney;
                picker.senderProfitMoney = sender.senderProfitMoney;
                senderOverViewBeanList.remove(sender);
            }
        }
        pickerOverViewBeanList.addAll(senderOverViewBeanList);//添加不重复数据
        for (PickerOverViewBean picker : pickerOverViewBeanList) {//合并重复数据
            picker = picker.getFormatPickerOverViewBean(picker);
        }
        return pickerOverViewBeanList;
    }

    @CommonResponse
    @PostMapping(value = "/uploadRedPackage1")
    @ApiOperation(value = "app上传红包详情", response = PayBean.class)
    public Object uploadRedPackage1(@RequestBody BeforeRedPackageBean redPackageBean) {
        if (redPackageBean.groupName != null && !"".equals(redPackageBean.groupName)) {
            redPackageBean.groupName = EmojiConverter.getInstance().toUnicode(redPackageBean.groupName);
        }
        RPResultToApp rpResultToApp = new RPResultToApp();
        WorkedRedPackageBean dbWorkedRedPackageBean = BaseServiceHelper.getRedPackageSendMapper().selectRedPackageById(redPackageBean.msgId);
        if (dbWorkedRedPackageBean != null) {
//            rpResultToApp.sendString = "检测到转包" + "\n此包为错包不计入统计";
            rpResultToApp.sendString = "";
            return rpResultToApp;
        }
        String userName = ThreadContextUtil.get("userName").toString();
        GroupRules groupRules = redisCacheService.getObject(RedisKeysDefine.CALC_RULE.concat(userName), GroupRules.class);
        //未获取到配置，红包不处理
        if (groupRules == null) {
            rpResultToApp.sendString = "未获取到配置，按以下步骤操作"
                    + "\n1，登陆云算法官网"
                    + "\n2，打开賠賠率设置，设置群规"
                    + "\n\n此包为错包不计入统计";
            return rpResultToApp;
        }
        //功能开关界面设置出错
        WorkedRedPackageBean workedRedPackageBean = WorkedRedPackageUtils.checkFeatureSwitch(redPackageBean, groupRules.getFeatureSwitch());
        if (workedRedPackageBean.isErrorRedPackage) {
            rpResultToApp = new RPResultToApp();
            rpResultToApp.sendString = workedRedPackageBean.errorRedPackageInfo + "\n\n此包为错包不计入统计" + new String(Character.toChars(0x1F389));
            return rpResultToApp;
        }
        FunctionSwitchBean functionSwitchBean = JsonUtils.parseObject(groupRules.getFeatureSwitch(), FunctionSwitchBean.class);
        assert functionSwitchBean != null;
        //如果
        if (functionSwitchBean.playType == 0) {
            WorkedRedPackageBean forbidWorkedRedPackageBean = WorkedRedPackageUtils.getWorkedRedPackageBean(redPackageBean, groupRules.getPackageRule());
            rpResultToApp = PayInfoUtils.getForbidRPResultToApp(redPackageBean, forbidWorkedRedPackageBean, groupRules);
            MessageBeanOld messageBean = PayInfoUtils.getMessageBean(functionSwitchBean.playType, redPackageBean, forbidWorkedRedPackageBean, groupRules);
            if (!forbidWorkedRedPackageBean.isErrorRedPackage) {//非错包，计入统计
                asyncInvokeService.informIndexDPanel(forbidWorkedRedPackageBean, messageBean, functionSwitchBean.playType);
            } else {
                rpResultToApp.sendString = rpResultToApp.sendString + "\n\n此包为错包不计入统计";
            }
        } else if (functionSwitchBean.playType == 2) {
            //接龙頑珐收到红包，就要移除提醒信息
            if (groupRules.getNextSenderRule() == null || "".equals(groupRules.getNextSenderRule())) {
                rpResultToApp.sendString = "未获取到接龙頑珐配置"
                        + "\n1，登陆云算法官网"
                        + "\n2，打开賠设置-接龙頑珐"
                        + "\n\n此包为错包不计入统计";
                return rpResultToApp;
            }
            NextSenderRuleBean nextSenderRule = JsonUtils.parseObject(groupRules.getNextSenderRule(), NextSenderRuleBean.class);
            WorkedRedPackageBean nextSenderWorkedRedPackageBean = NextSenderUtils.getWorkedRedPackageBean(redPackageBean, nextSenderRule);
            rpResultToApp = PayInfoUtils.getNextSenderRPResultToApp(redPackageBean, nextSenderWorkedRedPackageBean, groupRules);
            if (!nextSenderWorkedRedPackageBean.isErrorRedPackage) {//非错包，计入统计
                MessageBeanOld messageBean = PayInfoUtils.getMessageBean(functionSwitchBean.playType, redPackageBean, nextSenderWorkedRedPackageBean, groupRules);
                asyncInvokeService.informIndexDPanel(nextSenderWorkedRedPackageBean, messageBean, functionSwitchBean.playType);
                if (nextSenderRule.nextSenderType != 4) {//不是双A頑珐，无需发送接包提醒等
                    NextSendRemindUtils.nextSendRemind(userName, nextSenderWorkedRedPackageBean, nextSenderRule);
                }

            } else {
                rpResultToApp.sendString = rpResultToApp.sendString + "\n\n此包为错包不计入统计";
            }
        }
        return rpResultToApp;
    }

    @CommonResponse
    @PostMapping(value = "/uploadRedPackageInfo")
    @ApiOperation(value = "app上传红包详情", response = PayBean.class)
    public Object uploadRedPackageInfo(@RequestBody AliRedPackageBeanApp aliRedPackageBeanApp) {
        long currentTime = System.currentTimeMillis();
        String user = ThreadContextUtil.get("user").toString();
        AliRedPackageBean aliRedPackageBean = RedPackageUtils.appBeanToServerBean(user, aliRedPackageBeanApp);
        try {
            aliRedPackageBean = RedPackageUtils.getWorkedAliRedPackageBean(aliRedPackageBean, user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        AliRPResultToApp aliRPResultToApp = new AliRPResultToApp();
        aliRPResultToApp.atUserID = aliRedPackageBeanApp.fromUId;
        aliRPResultToApp.errorString = aliRedPackageBean.errorString;
        aliRPResultToApp.sendString = aliRedPackageBean.sendString;
        aliRPResultToApp.kickUserIdList = aliRedPackageBean.kickUserIdList;
        for (AliRedPackagePickerInfo pickerInfo : aliRedPackageBean.redPackagePickerInfoList) {
            pickerInfo.robotUser = user;
            pickerInfo.pickTime = currentTime;
        }
        if (StringUtils.isEmpty(aliRPResultToApp.errorString)) {
            sendMapper.insertRedPackageSend(aliRedPackageBean);
            pickMapper.insertRedPackagePick(aliRedPackageBean.redPackagePickerInfoList);
        }
        System.out.println(aliRPResultToApp.errorString);
        System.out.println(aliRPResultToApp.sendString);
        System.out.println("---------------------------------");
        System.out.println("\n");
        return aliRPResultToApp;
    }

}
